<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <button>按钮 1</button>
  <button>按钮 2</button>
  <button>按钮 3</button>
  <script>
    // 获取所有按钮元素
    const buttons = document.querySelectorAll('button');
    buttons.forEach((button, index) => {
      // 为每个按钮添加点击事件
      button.addEventListener('click', function () {
        alert(index);
      });
    });
  </script>
</body>
</html>